<#assign ctx="${rc.contextPath}">
<html>
<head>
    <title>模板管理</title>
    <script type="text/javascript">
        $(document).ready(function () {
            var winH = $(window).height();
            var toolH = $(".toolbar").outerHeight();
            var crumbH = $(".breadcrumb").outerHeight();
            var tgH = (winH - toolH - crumbH) * 0.7;

            $('#tg').treegrid({
                rownumbers: false,
                url: '${ctx}/kit/code/templateTree',
                height: tgH,
                idField: 'id',
                treeField: 'fileName',
                fitColumns: true,
                singleSelect: true
            });
        });

        var editingId;
        function edit() {
            if (editingId != undefined) {
                $('#tg').treegrid('select', editingId);
                return;
            }
            var row = $('#tg').treegrid('getSelected');
            if (undefined == row || null == row) {
                $.messager.alert('提示', "没有选择需要编辑的节点!", "warning");
                return;
            }
            $('#fileName').empty();
            $('#fileName').val(row.fileName);
            if (row.isDirectory) {
                editingId = row.id;
                $('#tg').treegrid('beginEdit', editingId);
            } else {
                $.ajax({
                    type: "POST",
                    url: "${ctx}/kit/code/content",
                    data: {filePath: row.filePath, fileName: row.fileName},
                    dataType: "json",
                    success: function (result) {
                        if (result.status == 200) {
                            $('#content').empty();
                            $('#content').val(result.content);
                            $('#dialog').panel({title: row.fileName});
                            $('#dialog').dialog('open');
                        } else {
                            successMsg(result.message);
                        }
                    }
                });
            }
        }

        function save(flag) {
            var row = $('#tg').treegrid('getSelected');
            console.log(row);
            var data;
            if (flag) {
                $('#tg').treegrid('endEdit', editingId);
                $('#tg').treegrid('select', editingId);
                editingId = undefined;
                var newRow = $('#tg').treegrid('getSelected');
                data = {
                    filePath: row.filePath,
                    fileName: $('#fileName').val(),
                    isDirectory: row.isDirectory,
                    newFileName: row.fileName
                };
            } else {
                if ($("#frmEdit").form('validate') && checkForm("frmEdit")) {
                    data = {
                        content: $('#content').val(),
                        filePath: row.filePath,
                        fileName: row.fileName,
                        isDirectory: row.isDirectory,
                        newFileName: $('#fileName').val()
                    };
                } else {
                    return false;
                }
            }
            $.ajax({
                type: "POST",
                url: "${ctx}/kit/code/saveTemp",
                data: data,
                dataType: "json",
                success: function (result) {
                    if (!flag) {
                        $('#dialog').dialog('close');
                    }
                    successMsg(result.message);
                    $('#tg').treegrid("reload");
                }
            });
        }

        function cancel() {
            if (editingId != undefined) {
                $('#tg').treegrid('cancelEdit', editingId);
                editingId = undefined;
                $('#tg').treegrid("reload");
            }
        }

        function cancel(flag) {
            if (flag) {
                if (editingId != undefined) {
                    $('#tg').treegrid('cancelEdit', editingId);
                    editingId = undefined;
                    $('#tg').treegrid("reload");
                }
            } else {
                $('#dialog').dialog('close');
            }
        }

        function back() {
            window.history.go(-1);
        }

    </script>
</head>
<body>
<div class="breadcrumb"> 开发工具 > 模板编辑</div>

<div id="toolbar" class="toolbar">
    <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-edit" onclick="edit();"
       plain="true">编辑</a>
    <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-save" onclick="save(true);"
       plain="true">保存</a>
    <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-cancel" onclick="cancel(true);"
       plain="true">取消</a>
    <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-unAudit" onclick="back();"
       plain="true">返回</a>
</div>

<table id="tg">
    <thead>
    <tr>
        <th data-options="field:'id',hidden:true">文件名</th>
        <th data-options="field:'filePath',hidden:true">地址</th>
        <th data-options="field:'isDirectory',hidden:true">是否为文件夹</th>
        <th data-options="field:'fileName',width:100,editor:'text'">文件名</th>
    </tr>
    </thead>
</table>

<div id="dialog" class="easyui-dialog" style="width:700px;height: 510px"
     data-options="iconCls:'icon icon-tpl',modal:true,closed:true">
    <form class="ued" id="frmEdit">
        <div>
            文件名：<span class="must">*</span>
            <input class="easyui-validatebox text" name="fileName" id="fileName"
                   data-options="required:true"/>
        </div>
        <div class="textarea">
            <textarea id="content" style="height: 350px;"></textarea>
        </div>
        <div>
            <input class="btn btn-primary" type="button" onclick="save(false)" value="保存"/>
            <input class="btn btn-primary" type="button" onclick="cancel(false)" value="取消"/>
        </div>
    </form>
</div>
</body>
</html>